package com.tceasy.gateway.dao
import com.tceasy.gateway.domain.OpenDeveloper
import com.tceasy.gateway.domain.OpenPermission
import com.tceasy.orm.base.GenericDao
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import org.springframework.util.Assert
/**
 * Created by zhanghuan on 2016/4/10.
 */
@Component
class DeveloperDao extends GenericDao<OpenDeveloper>{

    Logger logger=LoggerFactory.getLogger(DeveloperDao.class)

    /**
     * 根据开发者账号查询配置
     * @param partner
     * @return
     */
    public OpenDeveloper getByPartner(String partner){
        Assert.hasText(partner,"合作号不能为空")
        OpenDeveloper developer=findOneByPk(partner)
        Assert.notNull(developer,"不存在该合作账户")
        Assert.isTrue(developer.getState()!="N","该合作账户已关闭")
        return developer
    }

    /**
     * 查询某个开发者账号指定接口的权限配置
     * @param partner
     * @param method
     * @return
     */
    public OpenPermission getPermission(String partner,String method){
        Assert.hasText(partner,"合作号不能为空")
        Assert.hasText(method,"method不能为空")
        OpenPermission permission=findOneInline([partner: partner],"permission",[method:method],OpenPermission.class)
        return permission
    }

    /**
     * 判断某个开发者账号是否有权访问指定的接口
     * @param partner
     * @param method
     * @return true代表有权限，false代表无权限
     */
    public boolean hasPermission(String partner,String method){
        return getPermission(partner,method)!=null
    }
}
